package www.wen.com.common.lib.util

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import androidx.core.content.FileProvider
import java.io.File

/**
 * @author WEN
 * @Description: 调用系统的分享
 * @date 2022/3/2 20:26
 */
class SystemShareUtil {

    companion object {

        /**
         * 调用系统分享文件
         * @param file 分享文本
         * @param shareChannel 分享渠道
         * @param shareTitle 分享标题
         */
        @SuppressLint("ObsoleteSdkInt")
        @JvmStatic
        fun Activity.sharePdfFile(
            file: File,
            type: String = "application/pdf",
            authority: String = "${packageName}.fileProvider",
            shareChannel: String? = null,
            shareTitle: String = "分享到"
        ) {

            val intent = Intent(Intent.ACTION_SEND)
            intent.type = type
            val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                FileProvider.getUriForFile(this, authority, file)
            } else {
                Uri.fromFile(file)
            }
            shareChannel?.run { intent.setPackage(this); }
            intent.putExtra(Intent.EXTRA_STREAM, uri)
            startActivity(Intent.createChooser(intent, shareTitle))
        }

        /**
         * 调用系统分享文本
         * @param shareText 分享文本
         * @param shareChannel 分享渠道
         * @param shareTitle 分享标题
         */
        @JvmStatic
        fun Activity.shareText(
            shareText: String,
            shareChannel: String? = null,
            shareTitle: String = "分享到"
        ) {
            val intent = Intent(Intent.ACTION_SEND)
            intent.type = "text/plain"
            shareChannel?.run { intent.setPackage(this); }
            intent.putExtra(Intent.EXTRA_TEXT, shareText)
            startActivity(Intent.createChooser(intent, shareTitle))
        }

    }

}